<?php

namespace app\common\model\xilumarket;

use think\Model;
use function fast\array_get;


class Topic extends Model
{
    // 表名
    protected $name = 'xilumarket_topic';

    // 追加属性
    protected $append = [
        'status_text',
        'top_status_text',
        'image_text',
        'area',
    ];

    public function getAreaAttr($value,$data){
        return [
            'province' => !empty($data['province_id']) ? Area::getNameById($data['province_id']) : '',
            'city' => !empty($data['city_id']) ? Area::getNameById($data['city_id']) : '',
            'region' => !empty($data['region_id']) ? Area::getNameById($data['region_id']) : '',
            'address' => $data['address'] ?? '',
        ];
    }
    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getTopStatusList()
    {
        return ['0' => __('Top_status 0'), '1' => __('Top_status 1')];
    }

    public function getImageTextAttr($value,$data){
        return isset($data['image']) ? cdnurl($data['image'],true) : "";
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTopStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['top_status']) ? $data['top_status'] : '');
        $list = $this->getTopStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function children(){
        return $this->hasMany(self::class,'pid','id');
    }

    /**
     * 帖子参与人数
     */
    public function getTopicUserCount(){
        return TopicForum::where('topic_id',$this->id)->group('user_id')->count("*");
    }

    /**
     * 加入的数量
     */
    public function getTopicJoinUserCount(){
        return TopicUser::where('topic_id',$this->id)->group('user_id')->count("*");
    }

    /**
     * 热门话题
     * @param int $pagesize
     */
    public static function topLists($pagesize = 6){
        $lists = self::field('weigh,createtime,updatetime',true)
            ->where('status','normal')
            ->where('top_status','1')
            ->order('top_status','desc')
            ->order('updatetime','desc')
            ->limit($pagesize)
            ->select();
        foreach ($lists as $list){
            $list->join_users = TopicForum::where('topic_id',$list->id)->group('user_id')->count(1);
        }
        return $lists;
    }


    /**
     * 话题列表
     * @param $params
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function lists($params,$userId=0){
        $pagesize = array_get($params,'pagesize',10);
        $where['status'] = 'normal';
        $where['pid'] = 0;
        if($top_status = array_get($params,'top_status')){
            $where['top_status'] = $top_status;
        }
        //搜索
        if($q = array_get($params,'q')){
            $where['name'] = ['like',"%$q%"];
        }
        return self::field("id,name,image, province_id,city_id,region_id,address")->with(['children'=>function($query){
            $query->field('weigh,createtime,updatetime',true);
        }])
        ->where($where)
        ->order('updatetime','desc')
        ->paginate($pagesize)
        ->each(function ($row) use($userId){
            $row->join_users = $row->getTopicUserCount();
            $row->is_join = $userId?TopicUser::isJoinTopic($row->id,$userId):false;
        });
    }
}
